﻿Public Class DemoData

#Region "Public Interface"
    Public Shared Function CreateDemoData() As Objects
        Dim myObjects As New Objects
        With myObjects
            'create the objects
            Dim myContact As ObjectTypeTemplate = CreateObjectTypeTemplate_Contact("Contact")
            Dim myJob As ObjectTypeTemplate = CreateObjecTypeTemplate_Job("Job")
            Dim myTable As ObjectTypeTemplate = CreateObjecTypeTemplate_Table("Table")

            'now we add the relations
            myContact.AddRleation(myJob, enmRelationType.RelationType_n_m)
            myJob.AddRleation(myContact, enmRelationType.RelationType_n_m)
            myJob.AddRleation(myTable, enmRelationType.RelationType_n_m)
            myTable.AddRleation(myJob, enmRelationType.RelationType_n_m)

            'Now We add it into the base object
            .AddObjectTypeTemplate(myContact)
            .AddObjectTypeTemplate(myJob)
            .AddObjectTypeTemplate(myTable)

        End With
        Return myObjects
    End Function

    Public Shared Sub SaveDataAsXML(ByVal anObjects As Objects, ByVal aXMLFilePath As String)
        ObjectSerializer.WriteObjectAsXML(anObjects:=anObjects, aXMLFilePath:=aXMLFilePath)
    End Sub

    Public Shared Function ReadDataFromXML(ByVal aXMLFilePath As String) As Objects
        Return ObjectSerializer.ReadObjectFromXML(aXMLFilePath:=aXMLFilePath)
    End Function
#End Region

#Region "ObjectTypeTemplate Table"
    Private Shared Function CreateObjectTypeTemplate_Contact(ByVal aName As String) As ObjectTypeTemplate
        Dim myOTT As New ObjectTypeTemplate
        With myOTT
            Dim myGuid As Guid = Guid.NewGuid
            .ID = myGuid
            .Name = aName
            .ImageName = .Name.Trim.ToLowerInvariant & ".png"
            .VisioShapeName = "Contact"
            'Create the Properties
            .AddProperties(CreateObjectTypePropertyTemplate_Contact(myGuid))
        End With
        Return myOTT
    End Function
    Private Shared Function CreateObjectTypePropertyTemplate_Contact(ByVal aObjectTypeTemplateGuid As Guid) As List(Of ObjectTypePropertyTemplate)
        Dim myProperties As New List(Of ObjectTypePropertyTemplate)
        With myProperties
            Dim mySort As Integer = 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="Firstname", aObjectTypeTemplateID:=aObjectTypeTemplateGuid, _
                                                  aDatatype:=GetType(String), aFieldDefinition:=enmFieldDefinitions.Text, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
            mySort += 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="Lastname", aObjectTypeTemplateID:=aObjectTypeTemplateGuid, _
                                                  aDatatype:=GetType(String), aFieldDefinition:=enmFieldDefinitions.Text, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
            mySort += 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="Phone Number", aObjectTypeTemplateID:=aObjectTypeTemplateGuid, _
                                                  aDatatype:=GetType(String), aFieldDefinition:=enmFieldDefinitions.Text, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
            mySort += 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="email", aObjectTypeTemplateID:=aObjectTypeTemplateGuid, _
                                                  aDatatype:=GetType(String), aFieldDefinition:=enmFieldDefinitions.Text, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
        End With
        Return myProperties
    End Function
#End Region

#Region "ObjectTypeTemplate Table"
    Private Overloads Shared Function CreateObjecTypeTemplate_Table(ByVal aName As String) As ObjectTypeTemplate
        Dim myOTT As New ObjectTypeTemplate
        With myOTT
            Dim myGuid As Guid = Guid.NewGuid
            .ID = myGuid
            .Name = aName
            .ImageName = .Name.Trim.ToLowerInvariant & ".png"
            .VisioShapeName = "Table"
            'Create the properties
            .AddProperties(CreateObjectTypePropertyTemplate_Table_Property(myGuid))
        End With
        Return myOTT
    End Function
    Private Shared Function CreateObjectTypePropertyTemplate_Table_Property(ByVal aTabGuid As Guid) As List(Of ObjectTypePropertyTemplate)
        'create the properties
        Dim myProperties As New List(Of ObjectTypePropertyTemplate)
        With myProperties
            Dim mySort As Integer = 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="Test", aObjectTypeTemplateID:=aTabGuid, _
                                                  aDatatype:=GetType(String), aFieldDefinition:=enmFieldDefinitions.Text, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
        End With
        Return myProperties
    End Function
#End Region

#Region "ObjectTypeTemplate Job"
    Private Shared Function CreateObjecTypeTemplate_Job(ByVal aName As String) As ObjectTypeTemplate
        Dim myOTT As New ObjectTypeTemplate
        With myOTT
            Dim myGuid As Guid = Guid.NewGuid
            .ID = myGuid
            .Name = aName
            .ImageName = .Name.Trim.ToLowerInvariant & ".png"
            .VisioShapeName = "Job"
            'Create the properties
            .AddProperties(CreateObjectTypePropertyTemplate_Job_Property(myGuid))
        End With
        Return myOTT
    End Function
    Private Shared Function CreateObjectTypePropertyTemplate_Job_Property(ByVal aTabGuid As Guid) As List(Of ObjectTypePropertyTemplate)
        'create the properties
        Dim myProperties As New List(Of ObjectTypePropertyTemplate)
        With myProperties
            Dim mySort As Integer = 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="JobName", aObjectTypeTemplateID:=aTabGuid, _
                                                  aDatatype:=GetType(String), aFieldDefinition:=enmFieldDefinitions.Text, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
            mySort += 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="Starttime", aObjectTypeTemplateID:=aTabGuid, _
                                                  aDatatype:=GetType(DateTime), aFieldDefinition:=enmFieldDefinitions.DateTime, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
            mySort += 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="Duration", aObjectTypeTemplateID:=aTabGuid, _
                                                  aDatatype:=GetType(Integer), aFieldDefinition:=enmFieldDefinitions.Number, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
            mySort += 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="PathToJob", aObjectTypeTemplateID:=aTabGuid, _
                                                  aDatatype:=GetType(String), aFieldDefinition:=enmFieldDefinitions.Text, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
            mySort += 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="JobFileName", aObjectTypeTemplateID:=aTabGuid, _
                                                  aDatatype:=GetType(String), aFieldDefinition:=enmFieldDefinitions.Text, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
            mySort += 10
            .Add(CreateObjectPropertyTypeTemplate(aName:="RunAsUser", aObjectTypeTemplateID:=aTabGuid, _
                                                  aDatatype:=GetType(String), aFieldDefinition:=enmFieldDefinitions.Text, _
                                                  anEmptyAllowed:=False, anIsSystem:=True, _
                                                  aSortOrder:=mySort))
        End With

        Return myProperties
    End Function
#End Region

#Region "Helper Functions"
    Private Shared Function CreateObjectPropertyTypeTemplate(ByVal aName As String, _
                                                      ByVal aObjectTypeTemplateID As Guid, _
                                                      ByVal aDatatype As Type, _
                                                      ByVal aFieldDefinition As enmFieldDefinitions, _
                                                      ByVal anEmptyAllowed As Boolean, _
                                                      ByVal aSortOrder As Integer, _
                                                      ByVal anIsSystem As Boolean) As ObjectTypePropertyTemplate
        Dim myP As New ObjectTypePropertyTemplate With _
            {.ID = Guid.NewGuid, _
             .ObjectTypeTemplate_ID = aObjectTypeTemplateID, _
             .Name = aName, _
             .Datatype = aDatatype.ToString, _
             .FieldDefinition = aFieldDefinition, _
             .EmptyAllowed = anEmptyAllowed, _
             .SortOrder = aSortOrder, _
             .IsSystem = anIsSystem}
        Return myP
    End Function
#End Region

End Class
